<?php
define(dbhost,"localhost");
define(dbusername,"root");
define(dbpassword,"1491");
define(dbname,"Mkart");


class ord_data{
	public function getData($dataobject){
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");
		

		
		$pagesize = $dataobject['shownum']; //每頁筆數
		$nowpage = $dataobject['nowpage']; //目前頁面
		

		
		
		if(!isset($nowpage)){
		$currentpage = 1 ;
		}else{
		$currentpage = $nowpage ;
		}
		$startnum = ($currentpage -1) * $pagesize;   //開始的筆數
		$startnum = $startnum < 0 ? 0 : $startnum;
		
		$query = "select * from kart_order order by order_id limit $startnum,$pagesize";
		$result = $db->query($query);
		while ($obj = mysqli_fetch_object($result)) {
		$ordno = $obj->order_id ;
		$price = $obj->order_price ;
		$ord_d = $obj->order_date ;
		$ord_t = $obj->order_time ;
		$ord_s = $obj->order_state ;

		//時間
		$ord_dt = substr($ord_d,0,4) . "-" .  substr($ord_d,4,2) . "-"  . substr($ord_d,6,2) . " " .  substr($ord_t,0,2) . ":" .  substr($ord_t,2,2) . ":"  . substr($ord_t,4,2)  ;
		//交易狀態
		switch($ord_s){
		case "S" :
		$state = "已付費，已確認送貨" ;    
		break;
		case "Y" :
		$state = "已付費，尚未送貨" ;    
		break;
		case "N" :
		$state = "尚未付費" ;    
		break;		
		}

		
        $data_array[] = array("oId"=>$ordno,"price"=>$price,"time"=>$ord_dt,"state"=>$state) ;
    	}
		$db->close();
		return $data_array;	
	}
	
	public function dataCount(){
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");
		
		$sql = "select order_id from kart_order" ;
		$result_page = $db->query($sql);
		
		$numrows = mysqli_num_rows($result_page); //總筆數
		$db->close();
		return $numrows ;		
	}
	
	public function getOrderMainData($dataObject){
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");

		$query = "select * from kart_order where order_id = '$dataObject' ";
		$result = $db->query($query);
		$db->close();
		return $result;
	}
	
	public function getOrdItemList($dataObject){
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");

		$query = "select * from order_item where order_id = '$dataObject' ";
		$result = $db->query($query);
		while ($obj = mysqli_fetch_object($result)) {
		$item_id = $obj->item_id ;
		$item_amount = $obj->item_amount ;
		$itemquery = "select * from product where id = '$item_id' ";	
		$itemResult = $db->query($itemquery);
		$item_obj = mysqli_fetch_object($itemResult);
		$pd_name = $item_obj->name ;
		$pd_count = $item_obj->pd_count ;
		$pd_price1 = $item_obj->price_1 ;
		$pd_price2 = $item_obj->price_2 ;
		
		$pd_amount = $item_amount * $pd_price2 ;

		$data_array[] = array("name"=>$pd_name,"item_amount"=>$item_amount,"price"=>$pd_price2,"amount"=>$pd_amount,"count"=>$pd_count) ;
		}		
		
		$db->close();
		return $data_array;
	}
	
	public function getShopperData($dataObject){
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");

		$query = "select * from order_shopper where order_id = '$dataObject' ";
		$result = $db->query($query);
		$db->close();
		return $result;		
	}
	
	public function getConsignee($dataObject){
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");

		$query = "select * from order_consignee where order_id = '$dataObject' ";
		$result = $db->query($query);
		$db->close();
		return $result;		
	}
	
	public function saveStateData($dataObject){
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");
		
		$id = $dataObject['id'];
		$state = $dataObject['state'];

		$query = "update kart_order set order_state = '$state' where order_id = '$id' ";
		$result = $db->query($query);
		$db->close();
		return $result;
	}
	

	
	public function gwSpcheck($process_time,$gwsr,$amount,$spcheck,$check_sum){
		$T=$process_time+$gwsr+$amount;	//算出認證用的字串
		$a = substr($T,0,1).substr($T,2,1).substr($T,4,1);//取出檢查碼的跳字組合 1,3,5 字元
		$b = substr($T,1,1).substr($T,3,1).substr($T,5,1);//取出檢查碼的跳字組合 2,4,6 字元
		$c = ( $check_sum % $T ) + $check_sum + $a + $b;//取餘數 + 檢查碼 + 奇位跳字組合 + 偶位跳字組合
		if($spcheck == $c) {
        	return '1';
    	}  else {
    		return '0';
    	} 		    
	}
	
	public function orderLogin($dataObject){
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");
		
		session_start();
		
		
		$account = $dataObject["account"];
		$password = $dataObject["pwd"];
		
		$sql = "select user_auth from kart_manage where user_account = '$account' and user_password = '$password' and user_valid = 'Y' ";
		$result = $db->query($sql);
		$obj = mysqli_fetch_object($result);
		$_SESSION['login_state'] = $obj->user_auth;
		$db->close();
		
		if($_SESSION['login_state'] != ""){
			return  $_SESSION['login_state'];			
		}else{
			return false;		
		}
		
				
	}
	
	public function getSesstion(){
		session_start();
		
		if($_SESSION['login_state'] != ""){
			return $_SESSION['login_state'];
		}else{
			return false ;
		}
	}
}
	

		
		
	
	
	


?>